//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.jeeplus.common.utils;

import com.jeeplus.common.properties.QiniuProperties;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Client;
import com.qiniu.http.Response;
import com.qiniu.processing.OperationManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import com.qiniu.util.UrlSafeBase64;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.UUID;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class QiNiuUtils {
    private static final Logger logger = LoggerFactory.getLogger(QiNiuUtils.class);
    public static final String DOMAIN = QiniuProperties.getProperty("qiniu.domain");
    private static final String ACCESS_KEY = QiniuProperties.getProperty("qiniu.access_key");
    private static final String SECRET_KEY = QiniuProperties.getProperty("qiniu.secret_key");
    private static final String BUCKET_NAME = QiniuProperties.getProperty("qiniu.bucket");
    private static final String ZONE = QiniuProperties.getProperty("qiniu.zone");
    private static final String PIPELINE = QiniuProperties.getProperty("qiniu.pipeline", "admin_merge_radio");
    private static final Auth auth;
    private static final Configuration cfg;

    static {
        auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        Zone zone = Zone.autoZone();
        if("zone0".equals(ZONE)) {
            zone = Zone.zone0();
        } else if("zone1".equals(ZONE)) {
            zone = Zone.zone1();
        } else if("zone2".equals(ZONE)) {
            zone = Zone.zone2();
        } else if("zoneNa0".equals(ZONE)) {
            zone = Zone.zoneNa0();
        }

        cfg = new Configuration(zone);
    }

    public QiNiuUtils() {
    }

    public static String getUpToken() {
        return auth.uploadToken(BUCKET_NAME, (String)null, 360000L, (StringMap)null, true);
    }

    public static String getUpToken(String buketName) {
        return auth.uploadToken(buketName, (String)null, 360000L, (StringMap)null, true);
    }

    public static void upload(byte[] data, String fileName) {
        Response r;
        try {
            UploadManager uploadManager = new UploadManager(cfg);
            r = uploadManager.put(data, fileName, getUpToken());
            logger.info("�ϴ���Ӧ��Ϣ��" + r.bodyString());
        } catch (QiniuException var4) {
            r = var4.response;
            System.out.println(r.toString());
            throw new RuntimeException("�ϴ�ʧ��");
        }
    }

    public static JSONObject imageInfo(String url) {
        JSONObject result = new JSONObject();

        try {
            Client client = new Client();
            Response response = client.get(url + "?imageInfo");
            return JSONObject.fromObject(response.bodyString());
        } catch (QiniuException var4) {
            var4.printStackTrace();
            return result;
        }
    }

    public static String transfer(String url, String fileName) throws IOException {
        if(fileName == null) {
            fileName = UUID.randomUUID() + url.substring(url.lastIndexOf("."));
        }

        byte[] types = toByte(url);
        if(types == null) {
            return null;
        } else {
            upload(types, fileName);
            return DOMAIN + "/" + fileName;
        }
    }

    public static String transfer(String url) throws IOException {
        return transfer(url, (String)null);
    }

    private static byte[] toByte(String urlString) {
        try {
            URL url = new URL(urlString);
            URLConnection con = url.openConnection();
            con.setConnectTimeout(5000);
            InputStream is = con.getInputStream();
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buffer = new byte[4096];
            boolean var6 = false;

            int n;
            while(-1 != (n = is.read(buffer))) {
                output.write(buffer, 0, n);
            }

            is.close();
            return output.toByteArray();
        } catch (IOException var7) {
            logger.error(var7.getMessage(), var7);
            return null;
        }
    }

    public static String toMp4(String fileName, String newFileName) throws QiniuException {
        String pipeline = PIPELINE;
        String saveAs = UrlSafeBase64.encodeToString(BUCKET_NAME + ":" + newFileName);
        String fops = "avthumb/mp4/vcodec/libx264|saveas/" + saveAs;
        OperationManager operationMgr = new OperationManager(auth, cfg);
        String persistentId = operationMgr.pfop(BUCKET_NAME, fileName, fops, (new StringMap()).put("persistentPipeline", pipeline));
        return persistentId;
    }

    public static void main(String[] args) {
        imageInfo("http://or84r4lrb.bkt.clouddn.com/242b7b54-affc-4f72-b662-136c41b503a4.jpg");
    }
}
